package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.CommodityInfo;
import com.xuetang9.t9.hotel.beans.EConsumption;
import com.xuetang9.t9.hotel.beans.OrderFood;
import com.xuetang9.t9.hotel.mapper.IDiningTableMapper;
import com.xuetang9.t9.hotel.mapper.IEConsumptionMapper;
import com.xuetang9.t9.hotel.mapper.IOrderFoodMapper;
import com.xuetang9.t9.hotel.mapper.condition.impl.EConsumptionCondition;
import com.xuetang9.t9.hotel.services.IEConsumptionService;
import com.xuetang9.t9.hotel.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName: MyHotelProject
 * @Package: com.xuetang9.t9.hotel.service.impl
 * @ClassName: EconsumptionService
 * @Author: ChinaHp
 * @Description: 餐饮服务层实现类
 * @Date: 2020/2/24 16:23
 * @Version: 1.0
 */
@Service
public class EConsumptionService implements IEConsumptionService {

    @Autowired
    private IEConsumptionMapper mapper;
    @Autowired
    private IOrderFoodMapper orderFoodMapper;
    @Autowired
    private IDiningTableMapper tableMapper;

    @Override
    public boolean addAfterJoinEat(EConsumption ec) {
        mapper.insertAfterJoinEat(ec);
        int id = ec.getId();
        if(ec.getOrderFoods() != null){
            ec.getOrderFoods().forEach(orderFood -> {
                orderFoodMapper.insert(new OrderFood(id,orderFood.getFoodId(),orderFood.getCount()));
            });
        }
        tableMapper.updateTableState(ec.getTablesId());
        return id != 0;
    }

    @Override
    public boolean addNowJoinEat(EConsumption ec) {
        mapper.insertNowJoinEat(ec);
        int id = ec.getId();
        if(ec.getOrderFoods() != null) {
            ec.getOrderFoods().forEach(orderFood -> {
                orderFoodMapper.insert(new OrderFood(id, orderFood.getFoodId(), orderFood.getCount()));
            });
        }
        tableMapper.updateTableState(ec.getTablesId());
        return id != 0;
    }

    @Override
    public boolean addFreeJoinEat(EConsumption ec) {
        mapper.insertFreeJoinEat(ec);
        int id = ec.getId();
        if(ec.getOrderFoods() != null) {
            ec.getOrderFoods().forEach(orderFood -> {
                orderFoodMapper.insert(new OrderFood(id, orderFood.getFoodId(), orderFood.getCount()));
            });
        }
        tableMapper.updateTableState(ec.getTablesId());
        return id != 0;
    }

    @Override
    public List<EConsumption> getDealData(int currPage, int size) {
        return mapper.getDealData((currPage-1)*size, size);
    }

    @Override
    public List<EConsumption> dateFilter(String start, String end, int currPage, int size) {
        return mapper.dateFilter(start, end, currPage, size);
    }

    @Override
    public List<EConsumption> idFilter(int id, int currPage, int size) {
        return mapper.idFilter(id, currPage, size);
    }

    @Override
    public List<EConsumption> stateFilter(String state, int currPage, int size) {
        return mapper.stateFilter(state, currPage, size);
    }

    @Override
    public long getSumPage(int size) {
        long dataSize = mapper.dataSize();
        return dataSize % size == 0 ? dataSize / size : dataSize / size + 1;
    }

    @Override
    public Object getTodayIncome() {
        return mapper.getTodayIncome();
    }

    @Override
    public long getLjMoney() {
        return mapper.ljMoney();
    }

    @Override
    public long getJyMoney() {
        return mapper.jyMoney();
    }

    @Override
    public boolean update(EConsumption eConsumption) {
        return mapper.update(eConsumption) > 0;
    }

    @Override
    public EConsumption find(int id) {
        return mapper.select(id);
    }

    @Override
    public boolean addOuterEat(EConsumption eConsumption) {
        return mapper.insertOuterEat(eConsumption) > 0;
    }

    @Override
    public boolean addJoinEat(EConsumption eConsumption) {
        return mapper.insertJoinEat(eConsumption) > 0;
    }

    @Override
    public PageBean queryByAll(EConsumptionCondition consumptionCondition) {
        if(consumptionCondition == null){
            consumptionCondition = new EConsumptionCondition();
        }
        PageBean pageBean = new PageBean();
        pageBean.ok(mapper.selectByAll(consumptionCondition));
        pageBean.setCount(queryByCount(consumptionCondition));
        return pageBean;
    }

    @Override
    public Long queryByCount(EConsumptionCondition consumptionCondition) {
        return mapper.selectByCount(consumptionCondition);
    }

    @Override
    public List<EConsumption> queryAll() {
        return mapper.queryAll();
    }

    @Override
    public EConsumption selectByTableId(Integer id) {
        return mapper.selectByTableId(id);
    }

    @Override
    public EConsumption selectByBesides(Integer id) {
        return mapper.selectByBesides(id);
    }
}
